define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Panicauction, $window, $timeout, $ionicSideMenuDelegate, GoPage, $interval, $ionicSlideBoxDelegate) {
		
		$scope.selectIndex = 0;
		$scope.status = "started";//默认进行中
        $scope.datasArr = new Array();//存放一览数据
		$scope.nPageArr = [1,1];//页数
		
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				$scope.lan = 20;
			}
			$scope.all = $window.innerHeight;
			$scope.init_data();
		}, false);
		
		$scope.$on('$ionicView.beforeLeave',function(){
			if($scope.timer){
				$interval.cancel($scope.timer);
			}
		})
		
		//加载数据
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
			if($scope.selectIndex==1){
				$scope.status = "will";
			}else{
				$scope.status = "started";
			}
			if(!$scope.datasArr[$scope.selectIndex]){
				getList();
			}
		};
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};
		
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		
		//初始化活动
		function getList(){
			Panicauction.getList(1, $scope.status, function(data, now_time) {
//				console.log(data)
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.datasArr[$scope.selectIndex] = data;
					$scope.set_timer(now_time,data.act);
				} else {
					$scope.datasArr[$scope.selectIndex] = new Object();
				}
			});
		}
		
		//获取列表数据
		$scope.init_data = function() {
			
			getList();
			
			//轮播图
			Panicauction.getBannerList(function(data){
				if(data.result&&data.result.associate){
					$scope.banners = data.result.associate;
					$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
					$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
					$timeout(function(){//获取轮播图的高度，获取到的高度一般是最高的那张图片的高度
						var advsBox = document.getElementById("advsBox").clientHeight;
						$scope.myObj = {
							"height": $scope.all - 44 - 35 - advsBox - $scope.lan + "px"
						}
					},500)
				}else{
					$scope.myObj = {
						"height": $scope.all - 44 - 35 - $scope.lan + "px"
					}
				}
			})
		}
		
		
		$scope.set_timer = function(now_time,dataArr) {
			if(!dataArr||$scope.status == "will") return;
		  	$scope.timer = $interval(function() {
				for(var i = 0; i < dataArr.length; i++) {
					if(dataArr[i].now_time != undefined)
						dataArr[i].now_time = dataArr[i].now_time - 1;
					else
						dataArr[i].now_time = dataArr[i].end_time - now_time;
					dataArr[i].formate_time = $scope.formate_time(dataArr[i].now_time);
				}
			}, 1000)
		}
		
		$scope.init_time = function(data, now_time) {
			for(var i = 0; i < data.act.length; i++) {
				if(data.act[i].now_time != undefined)
					data.act[i].now_time = data.act[i].now_time - 1;
				else{
					if(data.act_status==1){
						data.act[i].now_time = data.act[i].now_time - now_time;
					}else{
						data.act[i].now_time = data.act[i].end_time - now_time;
					}
				}
				data.act[i].formate_time = $scope.formate_time(data.act[i].now_time);
			}
		}
		
		$scope.formate_time = function(time) {
			if(time > 0) {
				var day = parseInt(time / (3600 * 24));
				var hour = parseInt((time % (3600 * 24)) / 3600);
				if(hour < 10)
					hour = "0" + hour;
				var min = parseInt((time % 3600) / 60);
				if(min < 10)
					min = "0" + min;
				var second = time % 60;
				if(second < 10)
					second = "0" + second;
				return day + '天' + hour + '小时' + min + '分' + second + '秒';
			} else {
				return '0天0小时0分0秒';
			}
		}
		
		/****************上下拉刷新*****************/
		$scope.isLoad = false; //可以加载
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Panicauction.getList(1,$scope.status,function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.datasArr[$scope.selectIndex] = data;
					$scope.set_timer(now_time,data.act);
				} else {
					$scope.datasArr[$scope.selectIndex] = new Object();
				}
				$scope.nPageArr[$scope.selectIndex] = 1;
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		};
		
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Panicauction.getList($scope.nPageArr[$scope.selectIndex]+1, $scope.status, function(data, now_time) {
				if(data != null&&data.act) {
					if($scope.datasArr[$scope.selectIndex].act != undefined) {
						$scope.init_time(data, now_time);
						$scope.datasArr[$scope.selectIndex].act = $scope.datasArr[$scope.selectIndex].act.concat(data.act);
						$scope.nPageArr[$scope.selectIndex] += 1;
					} else {
						$scope.datasArr[$scope.selectIndex] = data;
					}
				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		};
		/****************上下拉刷新*****************/
		
		//跳转到商品详情页
		$scope.onshow_detail = function(act_id,goods_id,product_id) {
			GoPage.go_states("activitythree", {
				'goods_id': goods_id,
				'product_id': product_id,
				'act_id': act_id,
				"type":4
			});
		};
		
		//跳转到店铺首页
		$scope.go_store = function(store_id,$event){
			$event.preventDefault();//阻止冒泡
			GoPage.go_states("first", {
				'store_id': store_id
			});
		}
		
	}

	controller.$inject = ['$scope', 'Panicauction', '$window', '$timeout', '$ionicSideMenuDelegate', 'GoPage', '$interval', '$ionicSlideBoxDelegate'];

	return app.registerController("panicauctionctrl",controller);
});